var http=createRequestObject();
var uploader="";
var uploadDir="";
var dirname="";
var filename="";
var timeInterval="";
var idname="";
var uploaderId="";

function createRequestObject() {
	var obj;
	var browser = navigator.appName;
	if(browser == "Microsoft Internet Explorer"){
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
	else{
		return new XMLHttpRequest();
	}   
}
function traceUpload() {
	http.onreadystatechange = handleResponse;
	http.open("GET", 'imageupload.php?upl=upl&uploadDir='+uploadDir+'&dirname='+dirname+'&filename='+filename+'&uploader='+uploader); 
	http.send(null);   
}

function DeleteUpload(filename) 
{
	http.onreadystatechange = handleResponse;
	http.open("GET", 'imageupload.php?del=del&filename='+filename); 
	http.send(null);   
}

function handleResponse() 
{
	if(http.readyState == 4)
	{
		var response=http.responseText; 
		if(response.indexOf("File uploaded") != -1)
		{
			clearInterval(timeInterval);
			//document.getElementById('loading'+idname).innerHTML="";
		}
		document.getElementById(uploaderId).innerHTML=response;
	}
	else 
	{
		document.getElementById(uploaderId).innerHTML="Uploading File. Please wait...";
	}
}
function uploadFile(obj, dname) 
{
	uploadDir=obj.value;
	idname=obj.name;
	dirname=dname;
	if(check(uploadDir)==true)
	{
		filename=uploadDir.substr(uploadDir.lastIndexOf('\\')+1);
		uploaderId = 'uploader'+obj.name;
		uploader = obj.name;
		document.getElementById('formName'+obj.name).submit();
		timeInterval=setInterval("traceUpload()", 1500);
	}

}

function check(filename)
{
	var s=filename.match(/\.([^\.]+)$/);
	if(s[1]=='png' || s[1]=='jpeg' || s[1]=='jpg' || s[1]=='gif' || s[1]=='JPEG' || s[1]=='JPG') 
	{
		//alert('Wrong extension!');  
		return true;
	}
	alert("Invalid filename, please select another file"); 
	return false;
}